/***************************************************************************
 *   Copyright (C) 2007 by gaojian   *
 *   781102@vip.sina.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *      "http://bbs.dict.cn/vocabulary.php?export=xml&username=gaojian2008cnn&password=gao123jian"                                                                   *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "vocabularylist.h"
#include "settings.h"

#include <klocale.h>
#include <dom/dom_node.h>
#include <dom/dom_string.h>

#include <qstring.h>
#include <qbuffer.h>
#include <qobject.h>
#include <qtextcodec.h>
#include <qstringlist.h>
#include <qlistview.h>
#include <qpopupmenu.h>
#include <qprocess.h>
VocabularyList::VocabularyList(QWidget * parent, const char *name)
    : KListView(parent, name)
{
	lv_num = addColumn(i18n("*"));//添加第一个字段
	lv_title = addColumn(i18n("Vocabulary"));//添加第二个字段
	setResizeMode(QListView::LastColumn);//让最后一个字段适应listview控件的宽度
	setMaximumWidth(140);//设置qlistview控件最大宽度
	setAllColumnsShowFocus(true);
	popup = new QPopupMenu ( this  );
        popup->insertItem ( i18n("Refresh list") , this , SLOT ( Refresh() ) );
	popup->insertItem ( i18n("GoVocabulary"),this, SLOT ( slotGoWebVocabulary()));
	fetchVocabulary();
	QObject::connect(this, SIGNAL(rightButtonPressed(QListViewItem *, const QPoint &, int)), SLOT(slotPopupMenu(QListViewItem *,const QPoint &)));
	
setEnabled ( false);
}


VocabularyList::~VocabularyList()
{
delete popup;
popup = NULL;
}


	



//#include "vocabularylist.moc"

/*!
    \fn VocabularyList::fetchVocabulary()
 */
void VocabularyList::fetchVocabulary()
{
	if(""==kdictsettings.username)
	{qDebug("用户名为空");
		return;//用户名或密码为空就返回
	
	}
        buffer = new QBuffer;
	buffer->open(IO_WriteOnly);
	qDebug("fect");
	QString url="http://bbs.dict.cn/vocabulary.php?export=xml&username="+kdictsettings.username+"&password="+kdictsettings.userpassword;
	
	KIO::TransferJob *  job = KIO::get(KURL(i18n(url)).url(), false, false);
	
	QObject::connect( job, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(slotData(KIO::Job *, const QByteArray &)));
	QObject::connect(job, SIGNAL(result(KIO::Job *)), this,SLOT(slotResult(KIO::Job *)));
}





void VocabularyList::slotData(KIO::Job *job, const QByteArray &data)
{
    buffer->writeBlock(data.data(), data.size());
qDebug("buffer");
}

void VocabularyList::slotResult(KIO::Job *job)
{
	QByteArray arraydata= buffer->buffer();
	arraydata.detach();
	delete buffer;
	buffer = NULL;
	if ( job->error() )
	job->showErrorDialog( this   );
	
	job->kill(true);
	job = NULL;
	QString str = QString(arraydata).simplifyWhiteSpace();
	QTextCodec *codec = QTextCodec::codecForName("gb2312"); // get the codec for gb2312
	str = codec->toUnicode( str );
	QTextCodec *codec1 = QTextCodec::codecForName("utf8"); // get the codec for utf8
	str = codec1->fromUnicode( str);
	doc.loadXML(str);//DOM::Document加载得到的字符窜
	wordlist=doc.getElementsByTagName(DOM::DOMString("word"));//得到标签名为“word“的节点l列表
	
    
	for(int i=0;i<wordlist.length();i++)
	{
		DOM::Node node=wordlist.item( i );//得到节点
		DOM::DOMString word=node.firstChild().nodeValue();
		qDebug(word.string());
		stringlist.append(word.string());
	}
	addWord();
	
}

/*!
    \fn VocabularyList::addWord()
 */
void VocabularyList::addWord()
{
	int i=1;
	for ( QStringList::Iterator it = stringlist.begin(); it != stringlist.end(); ++it )
	{
	QListViewItem *viewitem = new QListViewItem( this, QString("%1").arg( i ), *it );
	i++;
	}
	setEnabled ( true );
}

void VocabularyList::slotPopupMenu(QListViewItem * item,const QPoint &point)
{
	popup->move ( x() + point.x()+5 ,y() + point.y()+10 );
	popup->exec();
}

 void VocabularyList::Refresh()
{
	setEnabled ( false);
	QListView::clear ();
	stringlist.clear();
	fetchVocabulary();
}


//#include "vocabularylist.moc"

/*!
    \fn VocabularyList::slotGoWebVocabulary()
 */
void VocabularyList::slotGoWebVocabulary()
{
	//QProcess::execute("firefox http://bbs.dict.cn/vocabulary.php");
	proc = new QProcess( this );

	proc->addArgument( "firefox" );
	proc->addArgument( "http://bbs.dict.cn/vocabulary.php" );
	proc->start();
}
